﻿------------------------------------------
--					[oUF]
--					  
--
--Used code of oUF_Jir
--
--
------------------------------------------


------------------------------------------
--Addon configuration is in config.lua!
------------------------------------------
 
 --get the addon namespace
local addon, ns = ...
local cfg = ns.cfg
local timers = {}
local _G = _G
local oUF = ns.oUF
local LabelUI = ns.LabelUI

local classcolor
local playername
local playerclass
local point = LabelUI.point
local GetTalentId = LabelUI.GetTalentId
local ShortNumber = LabelUI.ShortNumber

local texture = {}

local SpawnAura = LabelUI.SpawnAura
local UpdateBlizzardBufFrame = LabelUI.UpdateBlizzardBufFrame

local LoadConfig = function()
	playerclass = cfg.playerclass
	classcolor = cfg.classcolor
	playername = cfg.playername
	texture.statusbar = cfg.statusbar
	texture.buffborder = cfg.buffborder
	texture.textureBackground = cfg.textureBackground3
	texture.textureBar = cfg.textureBar
	texture.texturehilite = cfg.texturehilite2	
end

local PORTRAIT_EXCEPTIONS = {
	["character\\worgen\\male\\worgenmale.m2"] = 1,
	["character\\felorc\\male\\felorcmale.m2"] = 1,
	["creature\\woolyrhino\\woolyrhino.m2"] = 1,
	["creature\\woolyrhino\\woolyrhinomount.m2"] = 1,
	["creature\\northrendbearmount\\northrendbearmountarmored.m2"] = 1,
	["creature\\dragonman\\dragonman.m2"] = 1,
	["creature\\dragonman\\dragonman_var1.m2"] = 1,
	["creature\\facelessone\\facelessone.m2"] = 1,
	["creature\\facelessonecaster\\facelessonecaster.m2"] = 1,
	["creature\\facelessoneaquatic\\facelessoneaquatic.m2"] = 1,
	["creature\\pygmy\\pygmybase.m2"] = 1,
	["creature\\pygmy\\pygmyhunter.m2"] = 1,
	["creature\\pygmy\\pygmyroadie.m2"] = 1,
	["creature\\pygmy\\pygmyshaman.m2"] = 1,
	["creature\\pygmy\\pygmywarrior.m2"] = 1,
	["creature\\pygmy\\sandpygmy.m2"] = 1,
	["creature\\felbat\\felbat.m2"] = 1,
	["creature\\gnoll\\gnoll.m2"] = 1,
	["creature\\malygos\\malygos.m2"] = 1,
	["creature\\dragonhawk\\dragonhawkmount.m2"] = 1,
	["creature\\undeaddrake\\northrendundeaddrake.m2"] = 1,
	["creature\\netherdrake\\northrendnetherdrake.m2"] = 1,
	["creature\\shaman_thrall\\shaman_thrall.m2"] = 1,
	["creature\\bloodqueen\\bloodqueen.m2"] = 1,
	["creature\\ragnaros2\\ragnaros2.m2"] = 1,
	["character\\human\\female\\humanfemale_hd.m2"] = 1,
	["character\\dwarf\\male\\dwarfmale_hd.m2"] = 1,
	["creature\\saberon\\saberon.m2"] = 1,
	--[[["character\human\female\humanfemale_hd.m2"] = 1,
	["character\human\female\humanfemale_hd.m2"] = 1,
	["character\human\female\humanfemale_hd.m2"] = 1,
	["character\human\female\humanfemale_hd.m2"] = 1,
	["character\human\female\humanfemale_hd.m2"] = 1,
	["character\human\female\humanfemale_hd.m2"] = 1,
	["character\human\female\humanfemale_hd.m2"] = 1,
	["character\human\female\humanfemale_hd.m2"] = 1,
	["character\human\female\humanfemale_hd.m2"] = 1,
	["character\human\female\humanfemale_hd.m2"] = 1,]]--
}

local function PercentNumber(num)
	return ("%.1f "):format(num):gsub("%.?0+([ ])$", "%1").."%"
end

local function SplitNumber(num)
	--if cfg.SplitHealthNumbers then
		if(num >= 1e5) then
			while true do  
				num, k = string.gsub(num, "^(-?%d+)(%d%d%d)", '%1.%2')
				if (k==0) then break end
			end
		end
	--end
	return num
end

--custom tags
oUF.Tags.Events['levelequal'] = 'UNIT_LEVEL'
oUF.Tags.Methods['levelequal'] = function(unit)
	local c = UnitClassification(unit)
		if(c == 'worldboss') then
			return Hex(245/255, 144/255, 0) .. '+'
		elseif (c == 'elite' or c == 'rareelite')  then
			return UnitLevel(unit) .. "+"
		elseif UnitLevel(unit) == UnitLevel("player") then 
			return
		else 
			return UnitLevel(unit)
		end
end

oUF.Tags.Events['status'] = 'UNIT_HEALTH'
oUF.Tags.Methods['status'] = function(unit)
	if(UnitIsDead(unit)) then
		return 'Dead'
	elseif(UnitIsGhost(unit)) then
		return 'Ghost'
	elseif(not UnitIsConnected(unit)) then
		return 'Offline'
	else
		return
	end
end


oUF.Tags.Events['Playeraltpower'] = 'UNIT_POWER'
oUF.Tags.Methods['Playeraltpower'] = function(unit)
	local cur = UnitPower(unit, ALTERNATE_POWER_INDEX)
	local max = UnitPowerMax(unit, ALTERNATE_POWER_INDEX)
	if(max > 0 and not UnitIsDeadOrGhost(unit)) then
		return ("%s%%"):format(math.floor(cur/max*100+.5))
	else
		return ''
	end
end

oUF.Tags.Events['perhp'] = 'UNIT_HEALTH'
oUF.Tags.Methods['perhp'] = function(unit)
	if(UnitIsDead(unit)) then
		return 'Dead'
	elseif(UnitIsGhost(unit)) then
		return 'Ghost'
	elseif(not UnitIsConnected(unit)) then
		return 'Offline'
	else
		local m = UnitHealthMax(unit)
		if(m == 0) then
			return 0
		else
			return (math.floor(UnitHealth(unit)/m*1000+.5)/10).."%"
		end
	end
end

oUF.Tags.Events['curhp'] = 'UNIT_HEALTH'
oUF.Tags.Methods['curhp'] = function(unit)
	if(UnitIsDead(unit)) then
		return 'Dead'
	elseif(UnitIsGhost(unit)) then
		return 'Ghost'
	elseif(not UnitIsConnected(unit)) then
		return 'Offline'
	else
		return ShortNumber(UnitHealth(unit))
	end
end

oUF.Tags.Events['maxhp'] = 'UNIT_HEALTH'
oUF.Tags.Methods['maxhp'] = function(unit)
	if(UnitIsDead(unit)) or (UnitIsGhost(unit)) or (not UnitIsConnected(unit)) then
		return
	else
		return ShortNumber(UnitHealthMax(unit))
	end
end

oUF.Tags.Events['curhpfull'] = 'UNIT_HEALTH'
oUF.Tags.Methods['curhpfull'] = function(unit)
	if(UnitIsDead(unit)) or (UnitIsGhost(unit)) or (not UnitIsConnected(unit)) then
		return
	else
		return SplitNumber(UnitHealth(unit))
	end
end

oUF.Tags.Events['maxhpfull'] = 'UNIT_HEALTH'
oUF.Tags.Methods['maxhpfull'] = function(unit)
	if(UnitIsDead(unit)) or (UnitIsGhost(unit)) or (not UnitIsConnected(unit)) then
		return
	else
		return SplitNumber(UnitHealthMax(unit))
	end
end

oUF.Tags.Events['curhpstr'] = 'UNIT_HEALTH'
oUF.Tags.Methods['curhpstr'] = function(unit)
	local str
	if(UnitIsDead(unit)) then
		return 'Dead'
	elseif(UnitIsGhost(unit)) then
		return 'Ghost'
	elseif(not UnitIsConnected(unit)) then
		return 'Offline'
	else
		local m = UnitHealthMax(unit)
		if(m == 0) then
			return ShortNumber(UnitHealth(unit)).." \| 0%"
		else
			return ShortNumber(UnitHealth(unit)).." \| "..PercentNumber(UnitHealth(unit)/m*100) --(math.floor(UnitHealth(unit)/m*1000+.5)/10).."%"
		end
	end
end

oUF.Tags.Events['curpower'] = 'UNIT_POWER'
oUF.Tags.Methods['curpower'] = function(unit)
	return ShortNumber(UnitPower(unit))
end

oUF.Tags.Events['maxpower'] = 'UNIT_POWER'
oUF.Tags.Methods['maxpower'] = function(unit)
	return ShortNumber(UnitPowerMax(unit))
end	

oUF.Tags.Events['curmana'] = 'UNIT_POWER'
oUF.Tags.Methods['curmana'] = function(unit)
	if UnitPowerType(unit) == 0 then return ShortNumber(UnitPower(unit)) end
end	

oUF.Tags.Events['maxmana'] = 'UNIT_POWER'
oUF.Tags.Methods['maxmana'] = function(unit)
	if UnitPowerType(unit) == 0 then return ShortNumber(UnitPowerMax(unit)) end
end	

oUF.Tags.Events['namecolor'] = 'UNIT_NAME_UPDATE'
oUF.Tags.Methods['namecolor'] = function(unit)
	local reaction = UnitReaction(unit, "player")
	local _, x = UnitClass(unit)
	if not UnitIsPlayer(unit) or not UnitIsFriend(unit, "player")
		then return Hex(oUF.colors.reaction[reaction])
	elseif (x)
		then	return Hex(_COLORS.class[x])
	else return
	end
end

oUF.Tags.Events['powercolor'] = 'UNIT_NAME_UPDATE'
oUF.Tags.Methods['powercolor'] = function(unit)
	local reaction = UnitReaction(unit, "player")
	local _, x = UnitClass(unit)
	if not UnitIsPlayer(unit)
		then return Hex(oUF.colors.reaction[reaction])
	elseif (x)
		then	return Hex(_COLORS.class[x])
	else return
	end
end

oUF.Tags.Events['nameshort'] = 'UNIT_NAME_UPDATE'
oUF.Tags.Methods['nameshort'] = function(unit)
	local str = UnitName(unit)
	--[[local pos
	if string.len(str)  > 25
	then 
		--if string.find(str, " ") and (string.find(str, " ") < 23)  then pos = string.find(str, " ") - 1 else pos = 23 end
		if strbyte(str, 23) == 208 or strbyte(str, 23) == 209 then pos = 22 else pos = 23 end
		str = string.sub(str, 1, pos)
		return str .. "..."
	else return str 
	end]]--
	return str
end

oUF.Tags.Methods["smartracecreature"] = function(u)
	if(UnitIsPlayer(u)) then
		return _TAGS['race'](u)
	end
	return _TAGS['creature'](u)
end


oUF.colors.runes = {
		-- Blood
		{0.81, 0.26, 0.1},
		-- Unholy
		{0.17, 0.8, 0.38},
		-- Frost
		{0.17, 0.73, 0.8},
		-- Death
		{0.89, 0.17, 0.8},
	}
	
oUF.colors.health = {143/255, 194/255, 32/255}

oUF.colors.happiness = {
		[1] = {182/255, 34/255, 32/255},
		[2] = {220/225, 180/255, 52/255},
		[3] = {143/255, 194/255, 32/255},
}	

oUF.colors.reaction = {
		[1] = {182/255, 34/255, 32/255},
		[2] = {182/255, 34/255, 32/255},
		[3] = {182/255, 92/255, 32/255},
		[4] = {220/225, 180/255, 52/255},
		[5] = {143/255, 194/255, 32/255},
		[6] = {143/255, 194/255, 32/255},
		[7] = {143/255, 194/255, 32/255},
		[8] = {143/255, 194/255, 32/255},
}	

oUF.colors.totems = {
		-- Fire
		{0.81, 0.26, 0.1},
		-- Earth
		{0.8, 0.72, 0.29},
		-- Water
		{0.17, 0.5, 1},
		-- Air
		{0.17, 0.73, 0.8},
	}
	
oUF.colors.shadoworbs = {83/255, 0, 147/255}
oUF.colors.lightforce = {0, 1, .59}
	
--[[function ShortNumber(num)
	if(num >= 1e6) then
		return (math.ceil( (num/1e6)) ) .. "m"
	elseif (num >= 1e3) then
		return (math.ceil( (num/1e3)) ) .. "k"
	else
		return num
	end
end]]--


--magic
local function menu(self)
	local dropdown = _G[string.format('%sFrameDropDown', string.gsub(self.unit, '(.)', string.upper, 1))]

	if dropdown then
		ToggleDropDownMenu(1, nil, dropdown, 'cursor')
	elseif (self.unit:match('party')) then
		ToggleDropDownMenu(1, nil, _G[format('PartyMemberFrame%dDropDown', self.id)], 'cursor')
	else
		FriendsDropDown.unit = self.unit
		FriendsDropDown.id = self.id
		FriendsDropDown.initialize = RaidFrameDropDown_Initialize
		ToggleDropDownMenu(1, nil, FriendsDropDown, 'cursor')
	end
end

local function tank_absorb(self, unit)
	self.Tank_absorb = CreateFrame('StatusBar', nil, self)

	self.Tank_absorb.bg = self.Tank_absorb:CreateTexture(nil, "BACKGROUND")
	self.Tank_absorb.bg:SetPoint(cfg.frames[unit].tank_absorb.pos.a1, point(cfg.frames[unit].tank_absorb.pos.af ,self), cfg.frames[unit].tank_absorb.pos.a2, cfg.frames[unit].tank_absorb.pos.x, cfg.frames[unit].tank_absorb.pos.y)
	self.Tank_absorb.bg:SetWidth(cfg.frames[unit].tank_absorb.width)
	self.Tank_absorb.bg:SetHeight(cfg.frames[unit].tank_absorb.height)

	self.Tank_absorb.bg:SetTexture(0, 0, 0, .4)
			
	local xspace = 0
	if cfg.frames[unit].tank_absorb.icon then
		xspace = cfg.frames[unit].tank_absorb.height - cfg.frames[unit].tank_absorb.border
	end
	self.Tank_absorb:SetPoint('TOPLEFT',self.Tank_absorb.bg,'TOPLEFT', cfg.frames[unit].tank_absorb.border + xspace, -cfg.frames[unit].tank_absorb.border)
	self.Tank_absorb:SetPoint('BOTTOMRIGHT',self.Tank_absorb.bg,'BOTTOMRIGHT', -cfg.frames[unit].tank_absorb.border, cfg.frames[unit].tank_absorb.border)
	self.Tank_absorb:SetStatusBarTexture(texture.statusbar)	
	self.Tank_absorb:GetStatusBarTexture():SetHorizTile(true)
	self.Tank_absorb:SetStatusBarColor(classcolor.r, classcolor.g, classcolor.b)
		
	if cfg.frames[unit].tank_absorb.icon then
		self.Tank_absorb.Icon = self.Tank_absorb:CreateTexture(nil, "OVERLAY")
		self.Tank_absorb.Icon:SetSize(cfg.frames[unit].tank_absorb.height - cfg.frames[unit].tank_absorb.border * 2, cfg.frames[unit].tank_absorb.height - cfg.frames[unit].tank_absorb.border * 2)
		self.Tank_absorb.Icon:SetPoint("TOPRIGHT", self.Tank_absorb, "TOPLEFT", -cfg.frames[unit].tank_absorb.border, 0)
		self.Tank_absorb.Icon:SetTexCoord(0.1,0.9,0.1,0.9)
	end	
	
	self.Tank_absorb.TextLeft = self.Tank_absorb:CreateFontString(nil, "OVERLAY")
    self.Tank_absorb.TextLeft:SetFont(cfg.font, cfg.frames[unit].tank_absorb.fontsize)
    self.Tank_absorb.TextLeft:SetShadowOffset(1, -1) 
    self.Tank_absorb.TextLeft:SetJustifyH('LEFT')
    self.Tank_absorb.TextLeft:SetPoint('LEFT',self.Tank_absorb,'LEFT', 2, 0)

	self.Tank_absorb.TextRight = self.Tank_absorb:CreateFontString(nil, "OVERLAY")
	self.Tank_absorb.TextRight:SetFont(cfg.font, cfg.frames[unit].tank_absorb.fontsize)
	self.Tank_absorb.TextRight:SetShadowOffset(1, -1) 
	self.Tank_absorb.TextRight:SetJustifyH('RIGHT')
	self.Tank_absorb.TextRight:SetWidth((cfg.frames[unit].tank_absorb.width - cfg.frames[unit].tank_absorb.height - cfg.frames[unit].tank_absorb.border * 2) * 0.75)
	self.Tank_absorb.TextRight:SetHeight(cfg.frames[unit].tank_absorb.height - cfg.frames[unit].tank_absorb.border * 2)
	self.Tank_absorb.TextRight:SetPoint('RIGHT',self.Tank_absorb,'RIGHT', -2, 0)
	
	self.Tank_absorb:Hide()
	
	local spell
	if playerclass == "WARRIOR" then
		spell, _, icon = GetSpellInfo(112048)
		self.Tank_absorb.Icon:SetTexture(icon)
	else
		return
	end
	local statusbar = self.Tank_absorb
	self.Tank_absorb.Updater = CreateFrame("Frame", nil, self) 
	self.Tank_absorb:SetScript('OnUpdate', function()
		local buff = {UnitBuff("player", spell, nil)}
		value1, value2 = select(14,unpack(buff))
		if value1 then
			statusbar:Show()
			statusbar.TextLeft:SetText(ShortNumber(value2))
			local name, rank, texture, count, dtype, duration, timeLeft = unpack(buff)
			if not statusbar.duration or duration ~= statusbar.duration or statusbar.endtime ~= timeLeft then
				statusbar:SetMinMaxValues(0, duration)
				statusbar.endtime = timeLeft
				statusbar.duration = duration
				statusbar.lasttime = nil
			end
			timeLeft = timeLeft - GetTime()
			if not statusbar.lasttime or timeLeft < statusbar.lasttime - 0.1 then
				statusbar:SetValue(timeLeft)
				if timeLeft > 60 then
					local sektime = math.floor(mod(timeLeft, 60))
					statusbar.TextRight:SetText(tostring(math.floor((timeLeft)/60))..":"..tostring(math.floor(sektime/10))..tostring(math.floor(mod(sektime, 10))))
				else
					statusbar.TextRight:SetText(tostring(math.floor(timeLeft)).."."..tostring(math.floor(mod(timeLeft*10, 10))))
				end
				statusbar.lasttime = timeLeft
			end
		else
			statusbar:Hide()
			statusbar.duration = nil
			statusbar.lasttime = nil
		end
	end)
	
	local isaura = function()
		value1 = select(14,UnitBuff("player", spell, nil))
		if value1 then
			statusbar:Show()	
		else
			statusbar:Hide()
			statusbar.duration = nil
			statusbar.lasttime = nil
		end
	end
	local isvis = function()
		local masteryIndex = GetSpecialization()
		local isTank = false
	
		if masteryIndex then
    		if playerclass == "DRUID" and masteryIndex == 2 then --need to review
				isTank = true
			elseif playerclass == "DEATHKNIGHT" and masteryIndex == 1 then
				isTank = true
			elseif playerclass == "PALADIN" and masteryIndex == 2 then
				isTank = true
			elseif playerclass == "WARRIOR" and masteryIndex == 3 then
				isTank = true
			elseif playerclass == "MONK" and masteryIndex == 1 then
				isTank = true
			else
				isTank = false
			end
		else
			isTank = false
		end
		if isTank then
			self.Tank_absorb:RegisterEvent("UNIT_AURA")
		else
			self.Tank_absorb:UnregisterEvent("UNIT_AURA")
		end
	end
	self.Tank_absorb:RegisterEvent('ACTIVE_TALENT_GROUP_CHANGED')
	self.Tank_absorb:RegisterEvent('PLAYER_TALENT_UPDATE')
	isvis()
	self.Tank_absorb:SetScript('OnEvent', function(self, event, ...)
		if event == "UNIT_AURA" then
			isaura()
		else
			isvis()
		end
	end)
end

local Layout = function(self, unit)
	self.colors = colors
	self.menu = menu
	self:RegisterForClicks('AnyUp')
	self:SetAttribute('*type2', 'menu')
	self:SetScript('OnEnter', UnitFrame_OnEnter)
	self:SetScript('OnLeave', UnitFrame_OnLeave)
	local lastmodel
	
	if (unit == 'player') or (unit == 'target') then
		self:SetHeight(cfg.frames[unit].height)
		self:SetWidth(cfg.frames[unit].width)
		Back = CreateFrame('Frame', nil, self)
		Back:SetPoint('TOPRIGHT', self)
		Back:SetPoint('TOPLEFT', self)
		Back:SetFrameStrata('BACKGROUND')
		Back:SetBackdrop(texture.textureBackground)
		Back:SetBackdropColor(0, 0, 0, 0.5)
		Back:SetHeight(cfg.frames[unit].height)
		
		if cfg.frames[unit].showportrait then
			local portrait = CreateFrame('PlayerModel', nil, self)
			portrait:SetScript('OnUpdate', function(self) 
				local model = self:GetModel()
				--if unit ~= 'player' and model ~= lastmodel then 
				--	print(model)
				--	lastmodel = model
				--end
				if model and PORTRAIT_EXCEPTIONS[model] then
					self:SetCamera(1)
				else
					self:SetCamera(0)
				end
			end)
			portrait:SetWidth(cfg.frames[unit].height)
			portrait:SetHeight(cfg.frames[unit].height)
			portrait.type = '3D'
			self.Portrait = portrait
		end
			
		self.Health = CreateFrame('StatusBar', nil, self)
		self.Health:SetBackdrop(texture.textureBar)
		self.Health:SetBackdropColor(0.18,0.18,0.18, 0.8)
		self.Health:SetFrameStrata('LOW')
		self.Health:SetStatusBarTexture(texture.statusbar)
		self.Health:SetStatusBarColor(0.28,0.28,0.28)
		self.Health.frequentUpdates = false
		self.Health.Smooth = true
		self.Health.colorClass = true	
		self.Health.colorTapping = true
		self.Health:SetHeight(cfg.frames[unit].heathbarheight)
		
		self.RaidIcon = self.Health:CreateTexture(nil, "OVERLAY")
		self.RaidIcon:SetPoint("TOP", self.Health, 0, 12)
		self.RaidIcon:SetHeight(24)
		self.RaidIcon:SetWidth(24)
			
		self.Health.TextLeft = self.Health:CreateFontString(nil, 'OVERLAY') 
		self.Health.TextLeft:SetFont(cfg.font, cfg.frames[unit].fontsize)
		self.Health.TextLeft:SetShadowOffset(0,-1.5)
		self:Tag(self.Health.TextLeft,'[nameshort]')
		--self:Tag(self.Health.TextLeft,'[curhpfull]')
		self.Health.TextLeft:SetWidth((cfg.frames[unit].width - cfg.frames[unit].height) * 0.6)
		self.Health.TextLeft:SetHeight(cfg.frames[unit].fontsize * 1.7)
		self.Health.TextLeft.frequentUpdates = true
		self.Health.TextLeft:SetJustifyH('LEFT')
		self.Health.TextLeft:SetPoint('LEFT',self.Health,'LEFT', 2, 0)
			
		self.Health.TextRight = self.Health:CreateFontString(nil, 'OVERLAY') 
		self.Health.TextRight:SetFont(cfg.font, cfg.frames[unit].fontsize - 5)
		self.Health.TextRight:SetShadowOffset(0,-1.5)
		self:Tag(self.Health.TextRight,'[curhpstr]')
		self.Health.TextRight.frequentUpdates = true
		self.Health.TextRight:SetJustifyH('RIGHT')
		self.Health.TextRight:SetPoint('RIGHT',self.Health,'RIGHT', -2, -1.5)
		
		self.Health:SetScript('OnUpdate', function(self) 
			self.TextLeft:SetWidth((cfg.frames[unit].width - cfg.frames[unit].height) - self.TextRight:GetWidth() - 3)
		end)
			
		self.Power = CreateFrame('StatusBar', nil, self)
		self.Power:SetBackdrop(texture.textureBar)
		self.Power:SetBackdropColor(0.165,0.165,0.165, 0.8)
		self.Power:SetFrameStrata('LOW')
		self.Power:SetPoint('TOPRIGHT', self.Health, 'BOTTOMRIGHT', 0, -2)
		self.Power:SetPoint('TOPLEFT', self.Health, 'BOTTOMLEFT', 0, -2)
		self.Power:SetStatusBarTexture(texture.statusbar)
		self.Power:SetStatusBarColor(0,0,0)
		self.Power.frequentUpdates = true
		self.Power.Smooth = true
		self.Power:SetHeight(cfg.frames[unit].height - (cfg.frames[unit].heathbarheight + cfg.frames[unit].barspace))
		--self.Power.colorClass = false
		self.Power.colorPower = true
		
		--icons
		self.Leader = self.Health:CreateTexture(nil, 'OVERLAY')
		self.Leader:SetPoint('TOPLEFT', self.Portrait, 0, 10 )
		self.Leader:SetHeight(14)
		self.Leader:SetWidth(14)
		self.MasterLooter = self.Health:CreateTexture(nil, 'OVERLAY')
		self.MasterLooter:SetPoint('TOPLEFT', self.Portrait, 15, 10 )
		self.MasterLooter:SetHeight(11)
		self.MasterLooter:SetWidth(11)
		self.LFDRole = self.Health:CreateTexture(nil, "OVERLAY")
		self.LFDRole:SetPoint('TOPRIGHT', self.Portrait, 0, 10)
		self.LFDRole:SetHeight(12)
		self.LFDRole:SetWidth(12)
		
		end
	
	if unit == 'player' then
		if cfg.frames[unit].showportrait then self.Portrait:SetPoint('TOPLEFT', self, 'TOPLEFT', self) end
		self.Health:SetPoint('TOPRIGHT', self)
		if cfg.frames[unit].showportrait then self.Health:SetPoint('TOPLEFT', self.Portrait, 'TOPRIGHT', 1, 0) else self.Health:SetPoint('TOPLEFT', self) end
		
		--generic player power text
		self.Power.TextRight = self.Health:CreateFontString(nil, 'OVERLAY') 
		self.Power.TextRight:SetFont(cfg.font, cfg.frames[unit].fontsize - 5)
		self.Power.TextRight:SetShadowOffset(1, -1) 
		self.Power.TextRight.frequentUpdates = true
		self:Tag(self.Power.TextRight,'[curpower]\/[maxpower]')
		self.Power.TextRight:SetJustifyH('RIGHT')
		self.Power.TextRight:SetPoint('RIGHT',self.Power,'RIGHT', -2, 0)
			
		self.Power.TextLeft = self.Health:CreateFontString(nil, 'OVERLAY') 
		self.Power.TextLeft:SetFont(cfg.font, cfg.frames[unit].fontsize - 5)
		self.Power.TextLeft:SetShadowOffset(1, -1) 
		self.Power.TextLeft.frequentUpdates = true
		self:Tag(self.Power.TextLeft,'[difficulty][smartlevel]|r [smartracecreature]')
		self.Power.TextLeft:SetJustifyH('LEFT')
		self.Power.TextLeft:SetPoint('LEFT',self.Power,'LEFT', 2, 0)
		
		self.Combat = self.Health:CreateTexture(nil, "OVERLAY")
		self.Combat:SetPoint('CENTER', self.Health, 'CENTER', 0, 0)
		self.Combat:SetHeight(26)
		self.Combat:SetWidth(26)
		self.Combat:SetVertexColor(255, 0, 0)
		
		
		self.Resting = self.Health:CreateTexture(nil, "OVERLAY")
		self.Resting:SetPoint('TOP', self.Health, 0, 5)
		self.Resting:SetHeight(12)
		self.Resting:SetWidth(12)

		self.Debuffs = SpawnAura(self, unit, "debufs")
		if cfg.frames.tracking.bufs.enable then
			self.Buffs = SpawnAura(self, "tracking", "bufs") 
		end

		self.AltPowerBar = CreateFrame("StatusBar", nil, self)
		self.AltPowerBar:SetBackdrop(texture.textureBar)
		self.AltPowerBar:SetBackdropColor(0.165,0.165,0.165)
		self.AltPowerBar:SetFrameStrata('LOW')
		self.AltPowerBar:SetSize(200, 22)
		self.AltPowerBar:SetStatusBarTexture(texture.statusbar)
		self.AltPowerBar:GetStatusBarTexture():SetHorizTile(false)
		self.AltPowerBar:SetStatusBarColor(0.28,0.28,0.28)
		self.AltPowerBar:SetPoint('CENTER',UIParent,'CENTER', 0, 471)
		self.AltPowerBar.Smooth = true
	
		self.AltPowerBar.borderAltPowerBar = self.AltPowerBar:CreateTexture(nil, "BACKGROUND")
		self.AltPowerBar.borderAltPowerBar:SetPoint("TOPLEFT", self.AltPowerBar, "TOPLEFT", -1, 1)
		self.AltPowerBar.borderAltPowerBar:SetSize(202, 24)
		self.AltPowerBar.borderAltPowerBar:SetTexture(0, 0, 0, 0.5)
	
		-- alt power bar text
		self.AltPowerBar.pbText = self.AltPowerBar:CreateFontString(nil, "Overlay")
		self.AltPowerBar.pbText:SetFont(cfg.font, cfg.frames[unit].fontsize - 5)
		self.AltPowerBar.pbText:SetShadowOffset(1, -1)
		self.AltPowerBar.pbText:SetJustifyH("RIGHT")
		self.AltPowerBar.pbText:SetPoint("CENTER", self.AltPowerBar, "CENTER", 0, 0)
	
		self:Tag(self.AltPowerBar.pbText, "[Playeraltpower]")
		
		if cfg.frames[unit].castbar then	
			self.Castbar = CreateFrame('StatusBar', nil, self)
			
			self.Castbar.bg = self.Castbar:CreateTexture(nil, "BACKGROUND")
			self.Castbar.bg:SetPoint(cfg.frames[unit].castbar.pos.a1, point(cfg.frames[unit].castbar.pos.af ,self), cfg.frames[unit].castbar.pos.a2, cfg.frames[unit].castbar.pos.x, cfg.frames[unit].castbar.pos.y)
			self.Castbar.bg:SetWidth(cfg.frames[unit].castbar.width)
			self.Castbar.bg:SetHeight(cfg.frames[unit].castbar.height)

			self.Castbar.bg:SetTexture(0, 0, 0, .5)
			
			local xspace = 0
			if cfg.frames[unit].castbar.icon then
				xspace = cfg.frames[unit].castbar.height - cfg.frames[unit].castbar.border
			end
			self.Castbar:SetPoint('TOPLEFT',self.Castbar.bg,'TOPLEFT', cfg.frames[unit].castbar.border + xspace, -cfg.frames[unit].castbar.border)
			self.Castbar:SetPoint('BOTTOMRIGHT',self.Castbar.bg,'BOTTOMRIGHT', -cfg.frames[unit].castbar.border, cfg.frames[unit].castbar.border)
			self.Castbar:SetStatusBarTexture(texture.statusbar)	
			self.Castbar:GetStatusBarTexture():SetHorizTile(true)
			self.Castbar:SetStatusBarColor(classcolor.r, classcolor.g, classcolor.b)
			
			if cfg.frames[unit].castbar.icon then
				self.Castbar.Icon = self.Castbar:CreateTexture(nil, "OVERLAY")
				self.Castbar.Icon:SetSize(cfg.frames[unit].castbar.height - cfg.frames[unit].castbar.border * 2, cfg.frames[unit].castbar.height - cfg.frames[unit].castbar.border * 2)
				self.Castbar.Icon:SetPoint("TOPRIGHT", self.Castbar, "TOPLEFT", -cfg.frames[unit].castbar.border, 0)
				self.Castbar.Icon:SetTexCoord(0.1,0.9,0.1,0.9)
			end

	 		self.Castbar.SafeZone = self.Castbar:CreateTexture(nil, "BACKGROUND")
			self.Castbar.SafeZone:SetTexture(texture.statusbar)
	 		self.Castbar.SafeZone:SetVertexColor(0.8, 0, 0)
			
			--[[self.Castbar.Spark = self.Castbar:CreateTexture(nil, "OVERLAY")
			self.Castbar.Spark:SetWidth(25)
			self.Castbar.Spark:SetHeight(self.Castbar:GetHeight()*1.5)	
			self.Castbar.Spark:SetBlendMode("ADD")]]--

	 		self.Castbar.Shield = CreateFrame("Frame", nil, self.Castbar)
			self.Castbar.Shield:SetAllPoints(self.Castbar)
			self.Castbar.Shield:SetBackdrop(texture.texturehilite)
			self.Castbar.Shield:SetBackdropColor(0, 0, 0, 0)
			self.Castbar.Shield:SetBackdropBorderColor(184/255, 148/255, 0, 1)
			self.Castbar.Shield:Hide()

			self.Castbar.Text = self.Castbar:CreateFontString(nil, 'OVERLAY')
			self.Castbar.Text:SetPoint('LEFT', self.Castbar, 2, 0)
			self.Castbar.Text:SetSize(cfg.frames[unit].castbar.width*0.7, cfg.frames[unit].castbar.height)
			self.Castbar.Text:SetFont(cfg.font, cfg.frames[unit].castbar.fontsize)
			self.Castbar.Text:SetJustifyH("LEFT")
			self.Castbar.Text:SetShadowOffset(1,-1)
			
			self.Castbar.Ping = self.Castbar:CreateFontString(nil, 'OVERLAY')
			self.Castbar.Ping:SetPoint('BOTTOMRIGHT', self.Castbar, 2, 0)
			self.Castbar.Ping:SetFont(cfg.font, cfg.frames[unit].castbar.fontsize - 5)
			self.Castbar.Ping:SetJustifyH("RIGHT")
			self.Castbar.Ping:SetShadowOffset(1,-1)

			self.Castbar.Time = self.Castbar:CreateFontString(nil, 'OVERLAY')
			self.Castbar.Time:SetPoint('RIGHT', self.Castbar, -2, 0)
			self.Castbar.Time:SetHeight(cfg.frames[unit].castbar.height)
			self.Castbar.Time:SetFont(cfg.font, cfg.frames[unit].castbar.fontsize)
			self.Castbar.Time:SetShadowOffset(1,-1)
			self.Castbar.Time:SetJustifyH("RIGHT")
			self.Castbar.CustomTimeText = function(_, t)
				self.Castbar.Time:SetText(("%.1f/%.1f"):format(self.Castbar.castIsChanneled and t or self.Castbar.max - t, self.Castbar.max))
			end
			self.Castbar.CustomDelayText = function(_, t)
				self.Castbar.Time:SetFormattedText("%.1f|cffff0000+%.1f|r/%.1f", self.Castbar.castIsChanneled and t or self.Castbar.max - t, self.Castbar.delay, self.Castbar.max)
			end
			
			self.Castbar.trueHide = self.Castbar.Hide
			self.Castbar.trueShow = self.Castbar.Show
			local anim = self.Castbar:CreateAnimationGroup()
			local alphaOut = anim:CreateAnimation("Alpha")
			alphaOut:SetChange(-1)
			alphaOut:SetDuration(1)
			alphaOut:SetOrder(1)
			anim:SetScript("OnFinished", function() 
				self.Castbar:trueHide()	
			end)

			--CastBar fading:

			self.Castbar.alphaOut = anim
			
			self.Castbar.Hide = function ()
				if self.Castbar.duration then
					self.Castbar:SetValue(self.Castbar.duration)
				end
				self.Castbar.alphaOut:Play()
			end
			self.Castbar.Show = function ()
				if self.Castbar.alphaOut:IsPlaying() then self.Castbar.alphaOut:Stop() end
				self.Castbar:trueShow()					
			end

			--Curent Cast ping (thx Quartz)
			
			local sendTime

			local caststart = function(self, event, unit)
				if unit ~= "player" and unit ~= "vehicle" then
					return
				end
				sendTime = GetTime()
			end
			self:RegisterEvent("UNIT_SPELLCAST_SENT", caststart)

			self.Castbar.PostCastStart = function(self, event, unit, spell)
				if not self.duration then return end
				
				if sendTime then
					local timeDiff = GetTime() - sendTime
					timeDiff = timeDiff > self.max and self.max or timeDiff

					self.Ping:SetText(tostring(floor(timeDiff * 1000)).."ms")
				end
			end
			
			--Swing frame
			
			self.Swing = CreateFrame("Frame", nil, self)
			self.Swing:SetWidth(cfg.frames[unit].castbar.width)
			self.Swing:SetHeight(3)
			self.Swing:SetPoint('BOTTOMLEFT', self.Castbar.bg, 'TOPLEFT', 0, 0)
			self.Swing.texture = texture.statusbar
			self.Swing.color = {classcolor.r, classcolor.g, classcolor.b, 1}
			self.Swing.textureBG = texture.texturehilite
			self.Swing.colorBG = {0, 0, 0, 0.5}
			self.Swing.bg = self.Swing:CreateTexture(nil, "BACKGROUND")
			self.Swing.bg:Hide()
			self.Swing.bg:SetAllPoints(self.Swing)
			self.Swing.bg:SetTexture(0, 0, 0, .5)
			
			--GCD frame

			self.GCD = CreateFrame("Frame",  nil, self)
			self.GCD:SetHeight(3)
			self.GCD:SetWidth(cfg.frames[unit].castbar.width)
			self.GCD:SetPoint('TOPLEFT', self.Castbar.bg, 'BOTTOMLEFT', 0, 0)
			self.GCD:SetBackdrop(texture.texturehilite)
			self.GCD:SetBackdropColor(0, 0, 0, 0.5)
			self.GCD:SetBackdropBorderColor(0, 0, 0, 0)   
			self.GCD.Spark = self.GCD:CreateTexture(nil, "OVERLAY")	
			self.GCD.Spark:SetBlendMode("ADD")
			self.GCD.Spark:SetWidth(25)
			self.GCD.Spark:SetHeight(self.GCD:GetHeight()*2.5)	
			self.GCD.Spark:SetBlendMode("ADD")	
		
		end
		
	end
		
	if unit == 'target' then
		if cfg.frames[unit].showportrait then self.Portrait:SetPoint('TOPRIGHT', self, 'TOPRIGHT', self) end
			self.Health:SetPoint('TOPLEFT', self, 'TOPLEFT')
		if cfg.frames[unit].showportrait then self.Health:SetPoint('TOPRIGHT', self.Portrait, 'TOPLEFT',-1,0) else self.Health:SetPoint('TOPRIGHT', self) end
			self.Health.colorReaction = true
			
			
		--target specific power text
		self.Power.TextRight = self.Health:CreateFontString(nil, 'OVERLAY') 
		self.Power.TextRight:SetFont(cfg.font, cfg.frames[unit].fontsize - 5)
		self.Power.TextRight:SetShadowOffset(1, -1) 
		self.Power.TextRight.frequentUpdates = true
		self:Tag(self.Power.TextRight,'[curpower]\/[maxpower]')
		self.Power.TextRight:SetJustifyH('RIGHT')
		self.Power.TextRight:SetPoint('RIGHT',self.Power,'RIGHT', -2, 0)
			
		self.Power.TextLeft = self.Health:CreateFontString(nil, 'OVERLAY') 
		self.Power.TextLeft:SetFont(cfg.font, cfg.frames[unit].fontsize - 5)
		self.Power.TextLeft:SetShadowOffset(1, -1) 
		self.Power.TextLeft.frequentUpdates = true
		self:Tag(self.Power.TextLeft,'[difficulty][smartlevel]|r [smartracecreature]')
		self.Power.TextLeft:SetJustifyH('LEFT')
		self.Power.TextLeft:SetPoint('LEFT',self.Power,'LEFT', 2, 0)
			
		self.Auras = SpawnAura(self, unit, "aura")
		if cfg.frames.tracking.debufs.enable then
			self.Debuffs = SpawnAura(self, "tracking", "debufs") 
		end
		self.Castbar = CreateFrame('StatusBar', nil, self)
		self.Castbar:SetFrameStrata("MEDIUM")
		self.Castbar:SetAllPoints(self.Power)
		self.Castbar:SetStatusBarTexture(texture.statusbar)	
		self.Castbar:GetStatusBarTexture():SetHorizTile(true)
		--self.Castbar:SetStatusBarColor(29/255, 63/255, 72/255, 1)	
		self.Castbar:SetStatusBarColor(180/255, 180/255, 180/255, 1)

		-- cast bar background
		self.Castbar.bg = self.Castbar:CreateTexture(nil, "BACKGROUND")
		self.Castbar.bg:SetAllPoints(self.Castbar)
		self.Castbar.bg:SetTexture(0, 0, 0, .8)

	 	self.Castbar.Shield = CreateFrame("Frame", nil, self.Castbar)
		self.Castbar.Shield:SetAllPoints(self.Castbar)
		self.Castbar.Shield:SetBackdrop(texture.texturehilite)
		self.Castbar.Shield:SetBackdropColor(0, 0, 0, 0)
		self.Castbar.Shield:SetBackdropBorderColor(184/255, 148/255, 0, 1)
		self.Castbar.Shield:Hide()

		-- cast bar spell name text
		self.Castbar.Text = self.Castbar:CreateFontString(nil, 'OVERLAY')
		self.Castbar.Text:SetPoint('LEFT', self.Castbar, 2, 0)
		self.Castbar.Text:SetSize(200, 22)
		self.Castbar.Text:SetFont(cfg.font, cfg.frames[unit].fontsize - 5)
		self.Castbar.Text:SetJustifyH("LEFT")
		self.Castbar.Text:SetShadowOffset(1,-1)
		--self.Castbar.Text:SetTextColor(0, 0, 0)

		-- cast bar spell time text
		self.Castbar.Time = self.Castbar:CreateFontString(nil, 'OVERLAY')
		self.Castbar.Time:SetPoint('RIGHT', self.Castbar, -3, 0)
		self.Castbar.Time:SetSize(75, 22)
		self.Castbar.Time:SetFont(cfg.font, cfg.frames[unit].fontsize - 5)
		self.Castbar.Time:SetShadowOffset(1,-1)
		--self.Castbar.Time:SetTextColor(1, 1, 1)
		self.Castbar.Time:SetJustifyH("RIGHT")
		self.Castbar.CustomTimeText = function(_, t)
			self.Castbar.Time:SetText(("%.1f/%.1f"):format(self.Castbar.castIsChanneled and t or self.Castbar.max - t, self.Castbar.max))
		end
	end
		
	if (unit == 'focustarget') or (unit == 'focus') or (unit == 'targettarget') then
		self:SetHeight(cfg.frames[unit].height)
		self:SetWidth(cfg.frames[unit].width)
		
		self.Back = CreateFrame('Frame', nil, self)
		self.Back:SetPoint('TOPRIGHT', self)
		self.Back:SetPoint('TOPLEFT', self)
		self.Back:SetFrameStrata('BACKGROUND')
		self.Back:SetBackdrop(texture.textureBackground)
		self.Back:SetBackdropColor(0, 0, 0, 0.5)
		self.Back:SetHeight(cfg.frames[unit].height)
		
		self.Health = CreateFrame('StatusBar', nil, self)
		self.Health:SetBackdrop(texture.textureBar)
		self.Health:SetBackdropColor(0.18,0.18,0.18, 0.8)
		self.Health:SetFrameStrata('LOW')
		self.Health:SetPoint('TOPRIGHT', self)
		self.Health:SetPoint('TOPLEFT', self)
		self.Health:SetStatusBarTexture(texture.statusbar)
		self.Health:SetStatusBarColor(0.28,0.28,0.28)
		self.Health.frequentUpdates = true
		self.Health:SetHeight(0.6*(cfg.frames[unit].height - 1.5))
		self.Health.Smooth = true
		self.Health.colorClass = true
		self.Health.colorReaction = true

		self.Power = CreateFrame('StatusBar', nil, self)
		self.Power:SetBackdrop(texture.textureBar)
		self.Power:SetBackdropColor(0.165,0.165,0.165, 0.8)
		self.Power:SetFrameStrata('LOW')
		self.Power:SetPoint('TOPRIGHT', self.Health, 'BOTTOMRIGHT', 0, -2)
		self.Power:SetPoint('TOPLEFT', self.Health, 'BOTTOMLEFT', 0, -2)
		self.Power:SetStatusBarTexture(texture.statusbar)
		self.Power:SetStatusBarColor(0,0,0)
		self.Power.frequentUpdates = true
		self.Power.Smooth = true
		self.Power:SetHeight(0.4*(cfg.frames[unit].height - 1.5))
		--self.Power.colorClass = false
		self.Power.colorPower = true
			
		self.RaidIcon = self.Health:CreateTexture(nil, "OVERLAY")
		self.RaidIcon:SetPoint("TOPRIGHT", self, 0, 16)
		self.RaidIcon:SetHeight(16)
		self.RaidIcon:SetWidth(16)
			
		self.Health.TextLeft= self.Health:CreateFontString(nil, 'OVERLAY') 
		self.Health.TextLeft:SetFont(cfg.font, cfg.frames[unit].fontsize)
		self.Health.TextLeft:SetShadowOffset(0,-1.5)
		self:Tag(self.Health.TextLeft,'[nameshort]')
		self.Health.TextLeft.frequentUpdates = true
		self.Health.TextLeft:SetJustifyH('LEFT')
		self.Health.TextLeft:SetPoint('LEFT',self.Health,'LEFT', 2, 0)
		self.Health.TextLeft:SetWidth(cfg.frames[unit].width * 0.75)
		self.Health.TextLeft:SetHeight((cfg.frames[unit].fontsize - 3) * 1.7)

		self.Health.TextRight = self.Health:CreateFontString(nil, 'OVERLAY') 
		self.Health.TextRight:SetFont(cfg.font, cfg.frames[unit].fontsize - 2)
		self.Health.TextRight:SetShadowOffset(0,-1.5)
		self:Tag(self.Health.TextRight,'[perhp]')
		self.Health.TextRight.frequentUpdates = true
		self.Health.TextRight:SetJustifyH('CENTER')
		self.Health.TextRight:SetPoint('RIGHT',self.Health,'RIGHT', -2, -0.5)
		
		self.Health:SetScript('OnUpdate', function(self) 
			self.TextLeft:SetWidth(cfg.frames[unit].width - self.TextRight:GetWidth() - 3)
		end)
	
		self.Power.TextRight = self.Health:CreateFontString(nil, 'OVERLAY') 
		self.Power.TextRight:SetFont(cfg.font, cfg.frames[unit].fontsize - 2)
		self.Power.TextRight:SetShadowOffset(1, -1) 
		self.Power.TextRight.frequentUpdates = true
		self:Tag(self.Power.TextRight,'[curpower]\/[maxpower]')
		self.Power.TextRight:SetJustifyH('RIGHT')
		self.Power.TextRight:SetPoint('RIGHT',self.Power,'RIGHT', -2, 0)

		if unit == 'targettarget' and cfg.frames[unit]["aura"].enable then
			self.Auras = SpawnAura(self, unit, "aura")
			self.Auras.disableCooldown = true --cooldown is buged for tt frame
		elseif unit == 'focus' and cfg.frames[unit]["debufs"].enable then
			self.Auras = SpawnAura(self, unit, "debufs")
		end	
	end
	
	if unit == 'pet' then
		self:SetHeight(cfg.frames[unit].height)
		self:SetWidth(cfg.frames[unit].width)

		self.Back = CreateFrame('Frame', nil, self)
		self.Back:SetPoint('TOPRIGHT', self)
		self.Back:SetPoint('TOPLEFT', self)
		self.Back:SetFrameStrata('BACKGROUND')
		self.Back:SetBackdrop(texture.textureBackground)
		self.Back:SetBackdropColor(0, 0, 0, 0.5)
		self.Back:SetHeight(cfg.frames[unit].height)
		
		self.Health = CreateFrame('StatusBar', nil, self)
		self.Health:SetBackdrop(texture.textureBar)
		self.Health:SetBackdropColor(0.18,0.18,0.18)
		self.Health:SetFrameStrata('LOW')
		self.Health:SetPoint('TOPRIGHT', self)
		self.Health:SetPoint('TOPLEFT', self)
		self.Health:SetStatusBarTexture(texture.statusbar)
		self.Health:SetStatusBarColor(0.28,0.28,0.28)
		self.Health.colorHealth = true
		--self.Health.frequentUpdates = true
		self.Health.Smooth = true
		self.Health:SetHeight(0.6*(cfg.frames[unit].height - 1.5))
		--self.Health.colorHappiness = true

		self.Health.TextLeft= self.Health:CreateFontString(nil, 'OVERLAY') 
		self.Health.TextLeft:SetFont(cfg.font, cfg.frames[unit].fontsize)
		self.Health.TextLeft:SetShadowOffset(0,-1.5)
		self.Health.TextLeft:SetHeight(cfg.frames[unit].fontsize * 1.5)
		self.Health.TextLeft:SetWidth(cfg.frames[unit].width * 0.75)
		self:Tag(self.Health.TextLeft,'[nameshort]')
		self.Health.TextLeft.frequentUpdates = true
		self.Health.TextLeft:SetJustifyH('LEFT')
		self.Health.TextLeft:SetPoint('LEFT',self.Health,'LEFT', 2, 0)
			
		self.Health.TextRight = self.Health:CreateFontString(nil, 'OVERLAY') 
		self.Health.TextRight:SetFont(cfg.font, cfg.frames[unit].fontsize - 2)
		self.Health.TextRight:SetShadowOffset(0,-1.5)
		self:Tag(self.Health.TextRight,'[perhp]')
		self.Health.TextRight.frequentUpdates = true
		self.Health.TextRight:SetJustifyH('CENTER')
		self.Health.TextRight:SetPoint('RIGHT',self.Health,'RIGHT', -2, -0.5)
		
		self.Health:SetScript('OnUpdate', function(self) 
			self.TextLeft:SetWidth(cfg.frames[unit].width - self.TextRight:GetWidth() - 3)
		end)
				
		self.Power = CreateFrame('StatusBar', nil, self)
		self.Power:SetBackdrop(texture.textureBar)
		self.Power:SetBackdropColor(0.165,0.165,0.165)
		self.Power:SetFrameStrata('LOW')
		self.Power:SetPoint('TOPRIGHT', self.Health, 'BOTTOMRIGHT', 0, -2)
		self.Power:SetPoint('TOPLEFT', self.Health, 'BOTTOMLEFT', 0, -2)
		self.Power:SetStatusBarTexture(texture.statusbar)
		self.Power:SetStatusBarColor(0,0,0)
		self.Power.frequentUpdates = true
		--self.Power.Smooth = true
		self.Power:SetHeight(0.4*(cfg.frames[unit].height - 1.5))
		self.Power.colorPower = true

		self.Power.TextRight = self.Health:CreateFontString(nil, 'OVERLAY') 
		self.Power.TextRight:SetFont(cfg.font, cfg.frames[unit].fontsize - 2)
		self.Power.TextRight:SetShadowOffset(1, -1) 
		self.Power.TextRight.frequentUpdates = true
		self:Tag(self.Power.TextRight,'[curpower]\/[maxpower]')
		self.Power.TextRight:SetJustifyH('RIGHT')
		self.Power.TextRight:SetPoint('RIGHT',self.Power,'RIGHT', -2, 0)
		if cfg.frames[unit]["aura"].enable then 
			self.Auras = SpawnAura(self, unit, "aura")
		end		
		
		self.AltPowerBar = CreateFrame("StatusBar", nil, self)
		self.AltPowerBar:SetBackdrop(texture.textureBar)
		self.AltPowerBar:SetBackdropColor(0.165,0.165,0.165)
		self.AltPowerBar:SetFrameStrata('LOW')
		self.AltPowerBar:SetSize(200, 22)
		self.AltPowerBar:SetStatusBarTexture(texture.statusbar)
		self.AltPowerBar:GetStatusBarTexture():SetHorizTile(false)
		self.AltPowerBar:SetStatusBarColor(0.28,0.28,0.28)
		self.AltPowerBar:SetPoint('CENTER',UIParent,'CENTER', 0, 471)
		self.AltPowerBar.Smooth = true
	
		self.AltPowerBar.borderAltPowerBar = self.AltPowerBar:CreateTexture(nil, "BACKGROUND")
		self.AltPowerBar.borderAltPowerBar:SetPoint("TOPLEFT", self.AltPowerBar, "TOPLEFT", -1, 1)
		self.AltPowerBar.borderAltPowerBar:SetSize(202, 24)
		self.AltPowerBar.borderAltPowerBar:SetTexture(0, 0, 0, 0.5)
	
		-- alt power bar text
		self.AltPowerBar.pbText = self.AltPowerBar:CreateFontString(nil, "Overlay")
		self.AltPowerBar.pbText:SetFont(cfg.font, cfg.frames[unit].fontsize - 3)
		self.AltPowerBar.pbText:SetShadowOffset(1, -1)
		self.AltPowerBar.pbText:SetJustifyH("RIGHT")
		self.AltPowerBar.pbText:SetPoint("CENTER", self.AltPowerBar, "CENTER", 0, 0)
		
		self:Tag(self.AltPowerBar.pbText, "[Playeraltpower]")	
	end
		
	
	if cfg.frames[unit].runes and unit == "player" and select(2, UnitClass("player")) == "DEATHKNIGHT" then
		self.Runes = CreateFrame("Frame", nil, self)
		self.Runes:SetPoint(cfg.frames[unit].runes.pos.a1,point(cfg.frames[unit].runes.pos.af, self),cfg.frames[unit].runes.pos.a2, cfg.frames[unit].runes.pos.x, cfg.frames[unit].runes.pos.y)
		self.Runes:SetSize(unpack(cfg.frames[unit].runes.size))
		local RunesBG = self.Runes:CreateTexture(nil, "BACKGROUND")
		RunesBG:SetAllPoints(self.Runes)
		RunesBG:SetTexture(0, 0, 0, 0.5)
		self.Runes.anchor = cfg.frames[unit].runes.anchor
		self.Runes.growth = cfg.frames[unit].runes.grow
		self.Runes.height = cfg.frames[unit].runes.height
		self.Runes.width = cfg.frames[unit].runes.width
		local runemap = { 1, 2, 5, 6, 3, 4 }
			
		for i = 1, 6 do
			self.Runes[i] = CreateFrame("StatusBar", nil, self.Runes)
			self.Runes[i]:SetStatusBarTexture("Interface\\AddOns\\LabelUI\\media\\blank.blp")
			self.Runes[i]:SetSize(cfg.frames[unit].runes.width, cfg.frames[unit].runes.height)
			if cfg.frames[unit].runes.grow == 'UP' then
				if i > 1 then 
					self.Runes[i]:SetPoint("BOTTOM", self.Runes[i - 1], "TOP", 0, 0) 
				else
					self.Runes[i]:SetPoint("BOTTOM", self.Runes, "BOTTOM", 0, 0) 
					self.Runes[i]:SetPoint("TOP", self.Runes, "BOTTOM", 0, cfg.frames[unit].runes.height) 
				end	
			elseif cfg.frames[unit].runes.grow == 'DOWN' then
				if i > 1 then 
					self.Runes[i]:SetPoint("TOP", self.Runes[i - 1], "BOTTOM", 0, 0) 
				else
					self.Runes[i]:SetPoint("TOP", self.Runes, "TOP", 0, 0) 
					self.Runes[i]:SetPoint("BOTTOM", self.Runes, "TOP", 0, -cfg.frames[unit].runes.height) 
				end
			elseif cfg.frames[unit].runes.grow == 'RIGHT' then
				if i > 1 then 
					self.Runes[i]:SetPoint("LEFT", self.Runes[i - 1], "RIGHT", 0, 0) 
				else
					self.Runes[i]:SetPoint("LEFT", self.Runes, "LEFT", 0, 0) 
					self.Runes[i]:SetPoint("RIGHT", self.Runes, "LEFT", cfg.frames[unit].runes.width, 0) 
				end
			elseif cfg.frames[unit].runes.grow == 'LEFT' then
				if i > 1 then 
					self.Runes[i]:SetPoint("RIGHT", self.Runes[i - 1], "RIGHT", 0, 0) 
				else
					self.Runes[i]:SetPoint("RIGHT", self.Runes, "RIGHT", 0, 0) 
					self.Runes[i]:SetPoint("LEFT", self.Runes, "RIGHT", -cfg.frames[unit].runes.width, 0) 
				end
			end
			local backdrop = self.Runes[i]:CreateTexture(nil, "ARTWORK")
			backdrop:SetSize(cfg.frames[unit].runes.width, cfg.frames[unit].runes.height)
			backdrop:SetAllPoints()
			backdrop:SetTexture("Interface\\AddOns\\LabelUI\\media\\Combo1.tga")
			backdrop:SetBlendMode("BLEND")
			self.Runes[i]:SetID(i)
				
			-- This is actually the fill layer, but "bg" gets automatically vertex-colored by the runebar module. So let's make use of that!
			self.Runes[i].bg = self.Runes[i]:CreateTexture(nil, "OVERLAY")
			self.Runes[i].bg:SetSize(cfg.frames[unit].runes.width, cfg.frames[unit].runes.height)
			self.Runes[i].bg:SetPoint("BOTTOM")
			self.Runes[i].bg:SetTexture("Interface\\AddOns\\LabelUI\\media\\Combo3.tga")
				
			-- Shine effect
			local shinywheee = CreateFrame("Frame", nil, self.Runes[i])
			shinywheee:SetAllPoints()
			shinywheee:SetAlpha(0)
			shinywheee:Hide()
				
			local shine = shinywheee:CreateTexture(nil, "OVERLAY")
			shine:SetAllPoints()
			shine:SetPoint("CENTER")
			shine:SetTexture("Interface\\Cooldown\\star4.blp")
			shine:SetBlendMode("ADD")

			local anim = shinywheee:CreateAnimationGroup()
			local alphaIn = anim:CreateAnimation("Alpha")
			alphaIn:SetChange(0.3)
			alphaIn:SetDuration(0.4)
			alphaIn:SetOrder(1)
			local rotateIn = anim:CreateAnimation("Rotation")
			rotateIn:SetDegrees(-90)
			rotateIn:SetDuration(0.4)
			rotateIn:SetOrder(1)
			local scaleIn = anim:CreateAnimation("Scale")
			scaleIn:SetScale(2, 2)
			scaleIn:SetOrigin("CENTER", 0, 0)
			scaleIn:SetDuration(0.4)
			scaleIn:SetOrder(1)
			local alphaOut = anim:CreateAnimation("Alpha")
			alphaOut:SetChange(-0.5)
			alphaOut:SetDuration(0.4)
			alphaOut:SetOrder(2)
			local rotateOut = anim:CreateAnimation("Rotation")
			rotateOut:SetDegrees(-90)
			rotateOut:SetDuration(0.3)
			rotateOut:SetOrder(2)
			local scaleOut = anim:CreateAnimation("Scale")
			scaleOut:SetScale(-2, -2)
			scaleOut:SetOrigin("CENTER", 0, 0)
			scaleOut:SetDuration(0.4)
			scaleOut:SetOrder(2)
				
			anim:SetScript("OnFinished", function() shinywheee:Hide() end)
			shinywheee:SetScript("OnShow", function() anim:Play() end)
				
			-- Fill the dots
			self.Runes[i]:SetScript("OnValueChanged", function(self, des)
				_, statusMax = self:GetMinMaxValues()
				val = des / statusMax / 1.25
				--val = min(val, 1)
				--print(val)
				-- Resize round overlay texture when hidden statusbar changes
				local start, duration, runeReady = GetRuneCooldown(runemap[self:GetID()])
				--if start ~= 0 then
					if runeReady then
						-- Rune ready: show all 16x16px, play animation
						self.bg:SetSize(cfg.frames[unit].runes.width, cfg.frames[unit].runes.height)
						self.bg:SetTexCoord(0, 1, 0, 1)
						shinywheee:Show()
					else
						-- Dot distance from top & bottom of texture: 4px
						--self.bg:SetSize(cfg.frames[unit].runes.width, (cfg.frames[unit].runes.height / 5) + (cfg.frames[unit].runes.height * 2 / 3)  * val / 10)
						self.bg:SetSize(cfg.frames[unit].runes.width, cfg.frames[unit].runes.height * val)
						-- Show at least the empty 4 bottom pixels + val% of the 8 pixels of the actual dot = 12px max
						--self.bg:SetTexCoord(0, 1,  (cfg.frames[unit].runes.height * 4 / 5) / (cfg.frames[unit].runes.height) - (cfg.frames[unit].runes.height * 2 / 3) * val / 10 / (cfg.frames[unit].runes.height), 1)
						self.bg:SetTexCoord(0, 1, 1 - val, 1)
					end
				--end
			end)
		end
		local UpdateRune = function(self, event, rid)
			--print(rid)
			local frame = self:GetParent()
			--local rune = frame[rid]
			--print(rune.ID)
			for i = 1, 6 do
				local rune = frame[i]
				if(rune) then
					local start, duration, runeReady = GetRuneCooldown(runemap[rune:GetID()])
					--print(start, GetTime(), runeReady)
					if not (runeReady) then
						--if start > GetTime() then
							frame[runemap[rune:GetID()]].bg:SetSize(cfg.frames[unit].runes.width, cfg.frames[unit].runes.height)
							frame[runemap[rune:GetID()]].bg:SetTexCoord(0, 1, 1, 1)
						--end
					else					
						frame[runemap[rune:GetID()]].bg:SetSize(cfg.frames[unit].runes.width, cfg.frames[unit].runes.height)
						frame[runemap[rune:GetID()]].bg:SetTexCoord(0, 1, 0, 1)					
					end
				end
			end
		end
		self.Runes.Event = CreateFrame("Frame", nil, self.Runes)
		self.Runes.Event:RegisterEvent("RUNE_POWER_UPDATE")
		self.Runes.Event:RegisterEvent("RUNE_TYPE_UPDATE")
		self.Runes.Event:SetScript("OnEvent", UpdateRune)
	end

	if cfg.frames[unit].combopoints and unit == "target" and (select(2, UnitClass("player")) == "ROGUE" or select(2, UnitClass("player")) == "DRUID") then
		local CPFrame = CreateFrame("Frame", nil, self)
		local bg = {}
		local fill = {}
		self.CPoints = {}
		for i = 1, MAX_COMBO_POINTS do
			self.CPoints[i] = CreateFrame("Frame", nil, self)
			self.CPoints[i]:SetSize(16, 16)
			if i > 1 then self.CPoints[i]:SetPoint("LEFT", self.CPoints[i - 1], "RIGHT") end
			bg[i] = self.CPoints[i]:CreateTexture(nil, "ARTWORK")
			bg[i]:SetTexture("Interface\\AddOns\\LabelUI\\media\\Combo1.tga")
			bg[i]:SetAllPoints(self.CPoints[i])
			fill[i] = self.CPoints[i]:CreateTexture(nil, "OVERLAY")
			fill[i]:SetTexture("Interface\\AddOns\\LabelUI\\media\\Combo3.tga")
			if (select(2, UnitClass("player")) == "ROGUE") 
				then fill[i]:SetVertexColor(unpack(oUF.colors.class["ROGUE"]))
				else fill[i]:SetVertexColor(unpack(oUF.colors.class["DRUID"]))
			end
			fill[i]:SetAllPoints(self.CPoints[i])
		end
		self.CPoints[1]:SetPoint("TOPLEFT", self.Health, 0, 8)
		self.CPoints.unit = "player"
	end
	
	if cfg.frames[unit].totems and unit == "player" and select(2, UnitClass("player")) == "SHAMAN" then 
		self.Totems = CreateFrame("Frame", nil, self)
		self.Totems:SetPoint(cfg.frames[unit].totems.pos.a1,point(cfg.frames[unit].totems.pos.af, self),cfg.frames[unit].totems.pos.a2, cfg.frames[unit].totems.pos.x, cfg.frames[unit].totems.pos.y)		
		self.Totems:SetSize(unpack(cfg.frames[unit].totems.size))
		local TotemsBG = self.Totems:CreateTexture(nil, "BACKGROUND")
		TotemsBG:SetAllPoints(self.Totems)
		TotemsBG:SetTexture(0, 0, 0, 0.5)
		self.Totems.anchor = cfg.frames[unit].totems.anchor
		self.Totems.height = cfg.frames[unit].totems.height
		self.Totems.width = cfg.frames[unit].totems.width
		self.Totems.colors = {}
		self.Totems.colors[1] = oUF.colors.totems[FIRE_TOTEM_SLOT]
		self.Totems.colors[2] = oUF.colors.totems[EARTH_TOTEM_SLOT]
		self.Totems.colors[4] = oUF.colors.totems[WATER_TOTEM_SLOT]
		self.Totems.colors[3] = oUF.colors.totems[AIR_TOTEM_SLOT]
		
		for i = 1, 4 do
			self.Totems[i] = CreateFrame("Button", nil, self)
			if cfg.frames[unit].totems.grow == 'UP' then
				if i > 1 then 
					self.Totems[i]:SetPoint("BOTTOM", self.Totems[i - 1], "TOP", 0, 0) 
				else
					self.Totems[i]:SetPoint("BOTTOM", self.Totems, "BOTTOM", 0, 0) 
					self.Totems[i]:SetPoint("TOP", self.Totems, "BOTTOM", 0, cfg.frames[unit].totems.height) 
				end	
			elseif cfg.frames[unit].totems.grow == 'DOWN' then
				if i > 1 then 
					self.Totems[i]:SetPoint("TOP", self.Totems[i - 1], "BOTTOM", 0, 0) 
				else
					self.Totems[i]:SetPoint("TOP", self.Totems, "TOP", 0, 0) 
					self.Totems[i]:SetPoint("BOTTOM", self.Totems, "TOP", 0, -cfg.frames[unit].totems.height) 
				end
			elseif cfg.frames[unit].totems.grow == 'RIGHT' then
				if i > 1 then 
					self.Totems[i]:SetPoint("LEFT", self.Totems[i - 1], "RIGHT", 0, 0) 
				else
					self.Totems[i]:SetPoint("LEFT", self.Totems, "LEFT", 0, 0) 
					self.Totems[i]:SetPoint("RIGHT", self.Totems, "LEFT", cfg.frames[unit].totems.width, 0) 
				end
			elseif cfg.frames[unit].totems.grow == 'LEFT' then
				if i > 1 then 
					self.Totems[i]:SetPoint("RIGHT", self.Totems[i - 1], "RIGHT", 0, 0) 
				else
					self.Totems[i]:SetPoint("RIGHT", self.Totems, "RIGHT", 0, 0) 
					self.Totems[i]:SetPoint("LEFT", self.Totems, "RIGHT", -cfg.frames[unit].totems.width, 0) 
				end
			end
			self.Totems[i]:SetSize(cfg.frames[unit].totems.width, cfg.frames[unit].totems.height)
			self.Totems[i].bg = self:CreateTexture(nil)
			self.Totems[i]:SetScript('OnClick', function() end)
			
			local backdrop = self.Totems[i]:CreateTexture(nil, "ARTWORK")
			backdrop:SetSize(cfg.frames[unit].totems.width, cfg.frames[unit].totems.height)
			backdrop:SetAllPoints()
			backdrop:SetTexture("Interface\\AddOns\\LabelUI\\media\\Combo1.tga")
			
			local fill = self.Totems[i]:CreateTexture(nil, "OVERLAY")
			fill:SetSize(cfg.frames[unit].totems.width, cfg.frames[unit].totems.height)
			fill:SetPoint("BOTTOM", 0, 0)
			fill:SetTexture("Interface\\AddOns\\LabelUI\\media\\Combo3.tga")
			fill:SetVertexColor(unpack(oUF.colors.totems[i]))
			self.Totems[i].fill = fill
			
			local glowywheee = CreateFrame("Frame", nil, self.Totems[i])
			glowywheee:SetAllPoints()
			glowywheee:SetAlpha(0)
			glowywheee:Hide()

			
			local glow = glowywheee:CreateTexture(nil, "OVERLAY")
			glow:SetAllPoints()
			glow:SetPoint("CENTER")
			glow:SetTexture("Interface\\AddOns\\LabelUI\\media\\Combo2.tga")
			self.Totems[i].glow = glow

			local glowend = self.Totems[i]:CreateTexture(nil, "OVERLAY")
			glowend:SetAllPoints()
			glowend:SetTexture("Interface\\AddOns\\LabelUI\\media\\Combo2.tga")
			glowend:SetAlpha(0.5)
			glowend:Hide()
			self.Totems[i].glowend = glowend
			
			local anim = glowywheee:CreateAnimationGroup()
			local alphaIn = anim:CreateAnimation("Alpha")
			alphaIn:SetChange(0.5)
			alphaIn:SetSmoothing("OUT")
			alphaIn:SetDuration(1.5)
			alphaIn:SetOrder(1)

			glowywheee:SetScript("OnShow", function()
				glowend:Hide()
				anim:Play()
			end)
			
			anim:SetScript("OnFinished", function()
				glowend:Show()
				glowend:SetAlpha(0.5)
			end)
			self.Totems[i].glowywheee = glowywheee			
		end
		
		local HOLD = 0.2
		
		local on_update = function(self, elapsed)
			self.waited = self.waited + elapsed
			if self.waited >= HOLD then
				local timeleft = GetTotemTimeLeft(self:GetID())
				if timeleft then
					self.fill:SetSize(cfg.frames[unit].totems.width, (cfg.frames[unit].totems.height / 5 + 1) + timeleft / self.duration * (cfg.frames[unit].totems.height / 2 + 1))
					self.fill:SetTexCoord(0, 1, 0.77 - timeleft / self.duration / (cfg.frames[unit].totems.height) * (cfg.frames[unit].totems.height / 2 + 1), 1)					
					--totem.fill:SetVertexColor(self:GetStatusBarColor())
					if self.glowywheee:IsVisible() then
						totem.glowywheee:Hide()
						totem.glowend:Hide()
					end
					if not self.fill:IsVisible() then self.fill:Show() end
				else
					
				end
				self.waited = 0
			end
		end

		self.Totems.PostUpdate = function(self, slot, haveTotem, name, start, duration, icon)
			local totem = self[slot]
			if totem then
				if duration > 0 then
					-- Totem expired
					--totem.glow:SetVertexColor(totem:GetStatusBarColor())
					--totem.glowend:SetVertexColor(totem:GetStatusBarColor())
					totem:SetScript('OnUpdate', on_update)
					totem.waited = HOLD
					totem.duration = duration
				end
			end
		end
	end
	
	if cfg.frames[unit].classicons and unit == "player" and (playerclass == "PRIEST" or playerclass == "WARLOCK" or playerclass == "PALADIN" or playerclass == "MONK") then
		local maxClassIcons
		local IconColor
		local getLastIcon
		local bg = {}
		local CIconsAnim = {}
		self.ClassIcons = {}
		self.CIFrame = CreateFrame("Frame", nil, self)
		self.CIFrame:SetPoint(cfg.frames[unit].classicons.pos.a1,point(cfg.frames[unit].classicons.pos.af, self),cfg.frames[unit].classicons.pos.a2, cfg.frames[unit].classicons.pos.x, cfg.frames[unit].classicons.pos.y)
		self.CIFrame.anchor = cfg.frames[unit].classicons.anchor
		self.CIFrame.height = cfg.frames[unit].classicons.height
		self.CIFrame.width = cfg.frames[unit].classicons.width
		if playerclass == "PRIEST" then
			getLastIcon = function()
				return UnitPower("player", SPELL_POWER_SHADOW_ORBS)
			end
			self.CIFrame.lastCIcon = getLastIcon()
			maxClassIcons = UnitPowerMax('player', SPELL_POWER_SHADOW_ORBS)
			IconColor = oUF.colors.shadoworbs
		elseif playerclass == "WARLOCK" then
			getLastIcon = function()
				return UnitPower("player", SPELL_POWER_SOUL_SHARDS)
			end
			self.CIFrame.lastCIcon = getLastIcon()
			maxClassIcons = UnitPowerMax('player', ClassPowerID)
			IconColor = oUF.colors.power["SOUL_SHARDS"]
		elseif playerclass == "PALADIN" then
			getLastIcon = function()
				return UnitPower("player", SPELL_POWER_SOUL_SHARDS)
			end
			self.CIFrame.lastCIcon = getLastIcon()
			maxClassIcons = UnitPowerMax('player', SPELL_POWER_HOLY_POWER)
			IconColor = oUF.colors.power["HOLY_POWER"]
		elseif playerclass == "MONK" then
			getLastIcon = function()
				return UnitPower("player", SPELL_POWER_LIGHT_FORCE)
			end
			self.CIFrame.lastCIcon = getLastIcon()
			maxClassIcons = UnitPowerMax('player', SPELL_POWER_LIGHT_FORCE)
			IconColor = oUF.colors.lightforce
		end
		local function updatesize(maxicon)
			if cfg.frames[unit].classicons.grow == 'UP' or cfg.frames[unit].classicons.grow == 'DOWN' then
				self.CIFrame:SetSize(cfg.frames[unit].classicons.size, cfg.frames[unit].classicons.size * maxicon)	
			else
				self.CIFrame:SetSize(cfg.frames[unit].classicons.size * maxicon, cfg.frames[unit].classicons.size)	
			end
		end
		updatesize(maxClassIcons)
		local CIFrameBG = self.CIFrame:CreateTexture(nil, "BACKGROUND")
		CIFrameBG:SetAllPoints(self.CIFrame)
		CIFrameBG:SetTexture(0, 0, 0, 0.5)
		for i = 1, 5 do
			bg[i] = CreateFrame("Frame", nil, self)
			bg[i]:SetParent(self.CIFrame)
			if cfg.frames[unit].classicons.grow == 'UP' then
				if i > 1 then 
					bg[i]:SetPoint("BOTTOM", bg[i - 1], "TOP", 0, 0) 
				else
					bg[i]:SetPoint("BOTTOM", self.CIFrame, "BOTTOM", 0, 0) 
					bg[i]:SetPoint("TOP", self.CIFrame, "BOTTOM", 0, cfg.frames[unit].classicons.height) 
				end	
			elseif cfg.frames[unit].classicons.grow == 'DOWN' then
				if i > 1 then 
					bg[i]:SetPoint("TOP", bg[i - 1], "BOTTOM", 0, 0) 
				else
					bg[i]:SetPoint("TOP", self.CIFrame, "TOP", 0, 0) 
					bg[i]:SetPoint("BOTTOM", self.CIFrame, "TOP", 0, -cfg.frames[unit].classicons.height) 
				end
			elseif cfg.frames[unit].classicons.grow == 'RIGHT' then
				if i > 1 then 
					bg[i]:SetPoint("LEFT", bg[i - 1], "RIGHT", 0, 0) 
				else
					bg[i]:SetPoint("LEFT", self.CIFrame, "LEFT", 0, 0) 
					bg[i]:SetPoint("RIGHT", self.CIFrame, "LEFT", cfg.frames[unit].classicons.width, 0) 
				end
			elseif cfg.frames[unit].classicons.grow == 'LEFT' then
				if i > 1 then 
					bg[i]:SetPoint("RIGHT", bg[i - 1], "RIGHT", 0, 0) 
				else
					bg[i]:SetPoint("RIGHT", self.CIFrame, "RIGHT", 0, 0) 
					bg[i]:SetPoint("LEFT", self.CIFrame, "RIGHT", -cfg.frames[unit].classicons.width, 0) 
				end
			end
			bg[i]:SetSize(cfg.frames[unit].classicons.width, cfg.frames[unit].classicons.height)

			bg[i].tex = bg[i]:CreateTexture(nil, "ARTWORK")
			bg[i].tex:SetTexture("Interface\\AddOns\\LabelUI\\media\\Combo1.tga")
			bg[i].tex:SetAllPoints(bg[i])
			self.ClassIcons[i] = bg[i]:CreateTexture(nil, "OVERLAY")
			self.ClassIcons[i].bg = bg[i]
			self.ClassIcons[i]:SetTexture("Interface\\AddOns\\LabelUI\\media\\Combo3.tga")
			self.ClassIcons[i]:SetAllPoints(bg[i])
			self.ClassIcons[i]:SetVertexColor(unpack(IconColor))
			self.ClassIcons[i].SetVertexColor = function() return end
			
			
			CIconsAnim[i] = self.ClassIcons[i]:CreateAnimationGroup()
			local alphaIn = CIconsAnim[i]:CreateAnimation("Alpha")
			alphaIn:SetChange(1)
			alphaIn:SetSmoothing("OUT")
			alphaIn:SetDuration(1)
			alphaIn:SetOrder(1)
			
			CIconsAnim[i]:SetScript("OnFinished", function() self.ClassIcons[i]:SetAlpha(1) end)
		end
		
		local lastmax = 0
		self.ClassIcons.PostUpdate = function(_, cur, max, oldMax)
			if not max then
				return
			end
			local cIcon = max
			if cIcon > 0 then
				if self.CIFrame.lastCIcon <= cIcon then
					self.ClassIcons[cIcon]:SetAlpha(0)
					CIconsAnim[cIcon]:Play();
				end
			else
				for i = 1, maxClassIcons do
					self.CIFrame.lastCIcon = cIcon
					if CIconsAnim[i]:IsPlaying() then CIconsAnim[i]:Stop() end
				end
			end
			self.CIFrame.lastCIcon = cIcon
			for i=1, max do
				self.ClassIcons[i].bg:Show()
			end
			for i=max + 1, 5 do
				self.ClassIcons[i].bg:Hide()
			end
			if max ~= lastmax then
				updatesize(max)
				lastmax = max
			end
		end
		local isvis = function()
			if((playerclass == 'PRIEST' and SPEC_PRIEST_SHADOW ~= GetSpecialization())
				or (playerclass == 'WARLOCK' and not IsPlayerSpell(WARLOCK_SOULBURN))) then
				self.CIFrame:Hide()	
			else
				self.CIFrame:Show()
			end	
		end
		if(playerclass == 'PRIEST') then
			self.CIFrame:RegisterEvent('PLAYER_TALENT_UPDATE')
			isvis()
		elseif(playerclass == 'WARLOCK') then
			self.CIFrame:RegisterEvent('SPELLS_CHANGED')
			isvis()
		end
		self.CIFrame:SetScript('OnEvent', isvis)
	end
	
	if cfg.frames[unit].tank_absorb and unit == "player" and (playerclass == "DEATHKNIGHT" or playerclass == "WARRIOR" or playerclass == "PALADIN" or playerclass == "MONK") then
		tank_absorb(self, unit)
	end
			
	local hp = CreateFrame('StatusBar', nil, self.Health)
	hp:SetFrameLevel(self.Health:GetFrameLevel())
	hp:SetStatusBarTexture(texture.statusbar)
	hp:SetStatusBarColor(unpack(oUF.colors.reaction[5]))
	hp:SetWidth(cfg.frames[unit].width)
	hp:SetPoint('TOPLEFT', self.Health:GetStatusBarTexture(), 'TOPRIGHT', 0, 0)
	hp:SetPoint('BOTTOMLEFT', self.Health:GetStatusBarTexture(), 'BOTTOMRIGHT', 0, 0)
	
	local hpo = CreateFrame('StatusBar', nil, self.Health)
	hpo:SetFrameLevel(self.Health:GetFrameLevel())
	hpo:SetStatusBarTexture(texture.statusbar)
	hpo:SetStatusBarColor(unpack(oUF.colors.reaction[5]))
	hpo:SetWidth(cfg.frames[unit].width)
	hpo:SetPoint('TOPLEFT', self.Health:GetStatusBarTexture(), 'TOPRIGHT', 0, 0)
	hpo:SetPoint('BOTTOMLEFT', self.Health:GetStatusBarTexture(), 'BOTTOMRIGHT', 0, 0)
	
	self.HealPrediction = {
		myBar = hp,
		otherBar = hpo,
		maxOverflow = 1,
	}			
	
	self.Health.colorDisconnected = true
end		

local function Init()
	LoadConfig()
	oUF:RegisterStyle('LabelFrames', Layout)
	oUF:SetActiveStyle('LabelFrames')
	oUF:Spawn('player', 'oUF_Player'):SetPoint(cfg.frames.player.pos.a1,point(cfg.frames.player.pos.af),cfg.frames.player.pos.a2, cfg.frames.player.pos.x, cfg.frames.player.pos.y)
	oUF:Spawn('target', 'oUF_Target'):SetPoint(cfg.frames.target.pos.a1,point(cfg.frames.target.pos.af),cfg.frames.target.pos.a2, cfg.frames.target.pos.x, cfg.frames.target.pos.y)
	oUF:Spawn('targettarget', 'oUF_TOT'):SetPoint(cfg.frames.targettarget.pos.a1,point(cfg.frames.targettarget.pos.af),cfg.frames.targettarget.pos.a2, cfg.frames.targettarget.pos.x, cfg.frames.targettarget.pos.y)
	if cfg.frames.focus then
		oUF:Spawn('focus', 'oUF_Focus'):SetPoint(cfg.frames.focus.pos.a1,point(cfg.frames.focus.pos.af),cfg.frames.focus.pos.a2, cfg.frames.focus.pos.x, cfg.frames.focus.pos.y)
	end
	oUF:Spawn('pet', 'oUF_Pet'):SetPoint(cfg.frames.pet.pos.a1,point(cfg.frames.pet.pos.af),cfg.frames.pet.pos.a2, cfg.frames.pet.pos.x, cfg.frames.pet.pos.y)

	UpdateBlizzardBufFrame()
end;
LabelUI:AddInit(Init, "UnitFrames")
